esm-import-transformer
Can transform any ESM source code import
URLs using an import maps object. This package works in ESM or CJS.
import {html, css, LitElement} from "lit";
import {html, css, LitElement} from "https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js";
const {html, css, LitElement} = await import("lit");
const {html, css, LitElement} = require("lit");
Usage
import { ImportTransformer } from "esm-import-transformer";
const { ImportTransformer } = await import("esm-import-transformer");
Transform with an import map
Pass in a source code string and an import maps object.
let sourceCode = `import {html, css, LitElement} from "lit";`;
let it = new ImportTransformer(sourceCode);
let importMap = {
imports: {
lit: "https://cdn.jsdelivr.net/gh/lit/dist@2/core/lit-core.min.js"
}
};
let outputCode = it.transformWithImportMap(importMap);
Transform to dynamic import()
let sourceCode = `import {html, css, LitElement} from "lit";`;
let it = new ImportTransformer(sourceCode);
let outputCode = it.transformToDynamicImport();
Transform to require()
Added in v3.0.1: This method does not require that the downstream package is CommonJS, but just know that code will fail if you try to run it on a package that is not CommonJS.
let sourceCode = `import {html, css, LitElement} from "lit";`;
let it = new ImportTransformer(sourceCode);
let outputCode = it.transformToRequire();
Has imports?
Added in v3.0.2 Returns true if the code has any top level import
.
let sourceCode = `import {html, css, LitElement} from "lit";`;
let it = new ImportTransformer(sourceCode);
it.hasImports();
let sourceCode = `const {html, css, LitElement} = require("lit");`;
let it = new ImportTransformer(sourceCode);
it.hasImports();
Installation
Available on npm
npm install esm-import-transformer